﻿using _2112A_2_MES._chubowen;
using _2112A_2_MES._XiangHe;
using _2112A_2_MES.ChuBoWens;
using FourGroups.Project;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace _2112A_2_MES.ChuBoWen
{
    /// <summary>
    /// 生产退料表服务实现
    /// </summary>
    [ApiExplorerSettings(GroupName = "楚博文")]
    public class ProductionRejectionService : ApplicationService, ProductionRejectionRepository
    {
        /// <summary>
        /// 生产退料仓储
        /// </summary>
        private readonly IRepository<ProductionRejection, int> _productionrejectionRepository;
        public ProductionRejectionService(IRepository<ProductionRejection, int> productionrejectionRepository)
        {
            _productionrejectionRepository = productionrejectionRepository;
        }

        /// <summary>
        /// 获取生产退料表
        /// </summary>
        /// <param name="rtcode"></param>
        /// <param name="workordercode"></param>
        /// <param name="warehousename"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<InterSelect<object>> GetProductionRejectionList(string? rtcode, string? workordercode, string? warehousename, int pageIndex, int pageSize)
        {
            var productionrejectionList = await _productionrejectionRepository.GetListAsync();
            if (!string.IsNullOrEmpty(rtcode))
            {
                productionrejectionList = productionrejectionList.Where(x => x.RTCode.Contains(rtcode)).ToList();
            }
            if (!string.IsNullOrEmpty(workordercode))
            {
                productionrejectionList = productionrejectionList.Where(x => x.WorkOrderCode.Contains(workordercode)).ToList();
            }
            if (!string.IsNullOrEmpty(warehousename))
            {
                productionrejectionList = productionrejectionList.Where(x => x.WareHouseName.Contains(warehousename)).ToList();
            }
            var totalCount = productionrejectionList.Count;
            var pageList = productionrejectionList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            var productClassificationList = pageList.MapToList<ProductionRejection>();
            var data = new { TotalCount = totalCount, PageList = productClassificationList };
            if (productionrejectionList != null)
            {
                return new InterSelect<object>(true, "0", data);
            }
            return new InterSelect<object>(false, "1", null);
        }

        /// <summary>
        /// 新增生产退料表
        /// </summary>
        /// <param name="mditem"></param>
        /// <returns></returns>
        public async Task<int> AddProductionRejection(ProductionRejection productionRejection)
        {
            var result = await _productionrejectionRepository.FindAsync(x => x.RTCode == productionRejection.RTCode);
            if (result == null)
            {
                productionRejection.CreateBy = "楚博文";
                productionRejection.CreateTime = DateTime.Now;
                await _productionrejectionRepository.InsertAsync(productionRejection);
                return 1;
            }
            return 0;
        }

        /// <summary>
        /// 删除生产退料表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteProductionRejection(int id)
        {
            var result = await _productionrejectionRepository.FindAsync(id);
            if (result != null)
            {
                await _productionrejectionRepository.DeleteAsync(id);
                return 1;
            }
            return 0;
        }

        /// <summary>
        /// 获取物料产品
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ProductionRejection> GetProductionRejection(int id)
        {
            var result = await _productionrejectionRepository.FindAsync(id);
            if (result != null)
            {
                return result;
            }
            return null;
        }

        /// <summary>
        /// 修改物料产品
        /// </summary>
        /// <param name="mditem"></param>
        /// <returns></returns>
        public async Task<int> UpdateProductionRejection(ProductionRejectionDTO productionRejectionDTO)
        {
            var result = await _productionrejectionRepository.GetAsync(productionRejectionDTO.Id);
            result.RTCode = productionRejectionDTO.RTCode;
            result.RTName = productionRejectionDTO.RTName;
            result.WorkOrderId = productionRejectionDTO.WorkOrderId;
            result.WorkOrderCode = productionRejectionDTO.WorkOrderCode;
            result.WareHouseId = productionRejectionDTO.WareHouseId;
            result.WareHouseCode = productionRejectionDTO.WareHouseCode;
            result.LocationName = productionRejectionDTO.LocationName;
            result.RTDate = productionRejectionDTO.RTDate;
            result.Status = productionRejectionDTO.Status;
            result.UpdateBy = "小呆呆";
            result.UpdateTime = DateTime.Now;
            await _productionrejectionRepository.UpdateAsync(result);
            return result != null ? 1 : 0;
        }
    }
}
